package org.yibinxueyuan.drawcard.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yibinxueyuan.drawcard.entity.Cardinfo;
import org.yibinxueyuan.drawcard.entity.Userinfo;
import org.yibinxueyuan.drawcard.service.HandleLogin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录的控制
 */
@Controller
public class LoginController extends ApplicationObjectSupport {
    @Resource(name = "HandleLoginImpl")
    private HandleLogin handle;


    /**
     *  默认重定向到index页面
     */
    @RequestMapping(value = {"","/","/index"})
    public String index(Model m){
        Cardinfo cardinfoBean = (Cardinfo) this.getApplicationContext().getBean("cardinfoBean");
        cardinfoBean.setCardName("????");
        cardinfoBean.setCardAddress("https://www.ali213.net/zt/genshiimpact/sim/images/wz.png");

        String message = "这是主页";
        m.addAttribute("message",message);
        m.addAttribute("cardinfos",cardinfoBean);
        return "index";
    }

    /**
     * 如果没有登录,由其他页面跳转到登录页面
     */
    @RequestMapping(value = "/tologin")
    public String toLogin(Model m){
        String message = "请先登录后再访问";
        m.addAttribute("message",message);
        return "login/login";
    }


    @RequestMapping(value = "/tologinpower")
    public String toLoginPower(Model m){
        String message = "很抱歉,目前您没有权限访问此页面,请切换管理员账户后访问";
        m.addAttribute("message",message);
        return "login/login";
    }

    /**
     * 验证用户输入的参数是否正确
     */
    @RequestMapping(value = "/login")
    public String VerificationLogin(HttpServletRequest request, String mailbox, String password, Model m){
        try {
            //验证登录
            handle.VerificationLogin(mailbox,password);
//            HttpSession session = request.getSession();
//            session.setAttribute("mailbox",mailbox);
            Subject subject = SecurityUtils.getSubject();
            Userinfo user = (Userinfo) subject.getPrincipal();//拿到对象
            HttpSession session = request.getSession();
            session.setAttribute("nickname",user.getNickname());
            return "redirect:/index";
        }catch (UnknownAccountException e){ //用户不存在
            m.addAttribute("message","邮箱不存在");
            return "login/login";
        }catch (IncorrectCredentialsException e) { //密码错误
            m.addAttribute("message","密码与邮箱不匹配");
            return "login/login";
        }
    }
    /**
     *
     * 退出登录
     * */
    @RequestMapping(value = "loginout")
    public String loginOut(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/tologin";
    }

}
